package core;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import java.awt.Window;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;

import util.*;

public class MainGUI extends JFrame {
	
	NetClient NetClient;
	Client client;

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtPrice;
	private JTextField txtAmount;
	private JCheckBox chckbxNewCheckBox;

	private String stock;
	private String orderType;
	private float price;
	private int amount;
	private int id=0;
	private JComboBox<StockType> stockBox;
	private JComboBox<OrderType> operationBox;
	private JList<String> infoList;
	private JList<Order> orderList;

	private JScrollPane infoPanel;
	private JScrollPane orderPanel;
	private JTextField txtCancel;
	private JButton btnOrder;
	private JButton btnCancel;
	
	public MainGUI(Client client, final NetClient netClient) {
		this.NetClient = netClient;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 900, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		infoPanel = new JScrollPane();
		infoPanel.setBounds(5, 365, 885, 195);
		contentPane.add(infoPanel); 
		infoList = new JList<String>(netClient.getGuiChanger().getInfoModel());
		infoList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		infoPanel.setViewportView(infoList);
		
		orderPanel = new JScrollPane();
		orderPanel.setBounds(5, 5, 885, 330);
		contentPane.add(orderPanel); 
		orderList = new JList<Order>(netClient.getGuiChanger().getOrderModel());
		orderList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		orderPanel.setViewportView(orderList);
				
		stockBox = new JComboBox<>(StockType.values());
		stockBox.setBounds(10, 340, 90, 20);
		contentPane.add(stockBox);

		operationBox = new JComboBox<>(OrderType.values());
		operationBox.setBounds(110, 340, 90, 20);
		contentPane.add(operationBox);
		
		txtPrice = new JTextField();
		txtPrice.setText("100");
		txtPrice.setBounds(210, 340, 80, 20);
		contentPane.add(txtPrice);
		txtPrice.setColumns(10);

		chckbxNewCheckBox = new JCheckBox("MarketPrice");
		chckbxNewCheckBox.setBounds(300, 340, 80, 20);
		chckbxNewCheckBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (chckbxNewCheckBox.isSelected() ){
					txtPrice.setEnabled(false);
				} else {
					txtPrice.setEnabled(true);
				}
			}
		});
		contentPane.add(chckbxNewCheckBox);
		
		txtAmount = new JTextField();
		txtAmount.setText("10");
		txtAmount.setBounds(390, 340, 80, 20);
		contentPane.add(txtAmount);
		txtAmount.setColumns(10);
		
		btnOrder = new JButton("Order !");
		btnOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createOrder();
			}
		});
		btnOrder.setBounds(480, 340, 80, 20);
		contentPane.add(btnOrder);
		
		txtCancel = new JTextField();
		txtCancel.setText("id");
		txtCancel.setBounds(690, 340, 80, 20);
		contentPane.add(txtCancel);
		txtCancel.setColumns(10);
		
		btnCancel = new JButton("Cancel !");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createCancel();
			}
		});
		btnCancel.setBounds(780, 340, 100, 20);
		contentPane.add(btnCancel);
		
	
		
		setLocationRelativeTo(null);
		setResizable(false);
	}
	
	
	public void createOrder(){
		
		orderType = operationBox.getSelectedItem().toString();
		stock = stockBox.getSelectedItem().toString();
		
		if (!chckbxNewCheckBox.isSelected() ){
			try {
				price = Float.parseFloat(txtPrice.getText());
				if (price <= 0){
					txtPrice.setText("incorrect");
				}
				price = new BigDecimal(price).setScale(3, RoundingMode.HALF_DOWN).floatValue();
			} catch(NumberFormatException numExc) {
				txtPrice.setText("incorrect");
			}
		} else {
			price = orderType.equals("BID") ? 0.001f : Float.MAX_VALUE;
		}
			
		try {
			amount = Integer.parseInt(txtAmount.getText());
			if (amount <= 0){
				txtAmount.setText("incorrect");
			}
		} catch(NumberFormatException numExc) {
			txtAmount.setText("incorrect");
			return;
		}
		
		int orderId = id++;
		
		Order newOrder = new Order(orderId, stock, orderType, price, amount); 
		
		String order = "ORDER;" + orderId + ";" + orderType + ";" + stock + ";" + price + ";" + amount + ";"; 
		NetClient.getGuiChanger().getInfoModel().addElement(order);
		NetClient.getGuiChanger().getOrderModel().addElement(newOrder);
		NetClient.sendMessage(order);
	}

	private void createCancel() {
		//CANCEL;<id>;<OrderType: BID/ASK>,<StockName>;
		int cancelId;
		try {
			cancelId = Integer.parseInt(txtCancel.getText() ); 
			if (amount <= 0){
				txtCancel.setText("incorrect");
			}
		} catch(NumberFormatException numExc) {
			txtCancel.setText("incorrect");
			return;
		}
		Order order = NetClient.getGuiChanger().getOrderModel().get(cancelId);
		
		
		orderType = order.getOrderType();
		stock = order.getStock();
		
		String cancel = "CANCEL;" + cancelId + ";" + orderType + ";" + stock + ";";
		NetClient.getGuiChanger().getInfoModel().addElement(cancel);
		NetClient.sendMessage(cancel);
	}
}
